package com.cellulargames.view;

import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;

import com.cellulargames.model.Hand;

public class HandsPainter {

	public static Image hands[];
	
	public static Hand rightHand;
	
	public static Hand leftHand;
	
	public HandsPainter()throws Exception{		
		loadImages();		
		rightHand = new Hand(Hand.SELECCIONANDO, Hand.MANO_DERECHA);
		leftHand = new Hand(Hand.DESCANZANDO, Hand.MANO_IZQUIERDA);
	}
	
	public void pressed() {
		if( rightHand.state == Hand.SELECCIONANDO || rightHand.state == Hand.SENALANDO ){
			rightHand.press();
		}
		else if( leftHand.state == Hand.SELECCIONANDO || leftHand.state == Hand.SENALANDO ){			
			leftHand.press();			
		}
	}
	
	public void released() {
		if( rightHand.state == Hand.SELECCIONANDO ){
			rightHand.release();
		}
		else{
			leftHand.release();			
		}
	}
	
	public void senalar(int holdButton){
		if( holdButton >= 0 && holdButton <= 1 ){			
			leftHand.state = Hand.SENALANDO;
			rightHand.state = Hand.DESCANZANDO;
		}
		else{			
			leftHand.state = Hand.DESCANZANDO;
			rightHand.state = Hand.SENALANDO;
		}
	}
	
	public boolean isSenalando() {
		return ( leftHand.state == Hand.SENALANDO || rightHand.state == Hand.SENALANDO );
	}
	
	public void unSenala() {		
		leftHand.state = Hand.SELECCIONANDO;
		rightHand.state = Hand.DESCANZANDO;
	}
	
	private void loadImages()throws Exception {
		hands = new Image[4];
		hands[0] = Image.createImage("/hor.png");
		hands[1] = Image.createImage("/hol.png");
		hands[2] = Image.createImage("/hdr.png");
		hands[3] = Image.createImage("/hdl.png");
	}
	
	//8, 83, 157
	public void paint(Graphics g, int currentOption){
		
		g.drawImage(rightHand.getHand(), rightHand.xHand, rightHand.yHand, Graphics.LEFT|Graphics.TOP);
		g.drawImage(leftHand.getHand(), leftHand.xHand, leftHand.yHand, Graphics.LEFT|Graphics.TOP);		
		
		
		if( leftHand.state == Hand.SENALANDO || rightHand.state == Hand.SENALANDO ){
			
			switch( VideoPoker.holdButton ){
			
			case 0:
				if( leftHand.state == Hand.SENALANDO ){
					leftHand.senala(8);
					rightHand.descanza();
				}
				else{
					rightHand.senala(8);
					leftHand.descanza();
				}
				break;
				
			case 1:
				if( leftHand.state == Hand.SENALANDO ){
					leftHand.senala(45);
					rightHand.descanza();
				}
				else{
					rightHand.senala(45);
					leftHand.descanza();
				}
				break;
				
			case 2:
				if( leftHand.state == Hand.SENALANDO ){
					leftHand.senala(85);
					rightHand.descanza();
				}
				else{
					rightHand.senala(85);
					leftHand.descanza();
				}
				break;
				
			case 3:
				if( leftHand.state == Hand.SENALANDO ){
					leftHand.senala(140);
					rightHand.descanza();
				}
				else{
					rightHand.senala(135);
					leftHand.descanza();
				}
				break;
				
			case 4:
				if( leftHand.state == Hand.SENALANDO ){
					leftHand.senala(175);
					rightHand.descanza();
				}
				else{
					rightHand.senala(175);
					leftHand.descanza();
				}
				break;
				
			}
			
			return;
			
		}
		
		if( currentOption == 1 && VideoPoker.currentWin == 0 ){
			if( rightHand.state == Hand.DESCANZANDO ){
				leftHand.state = Hand.SELECCIONANDO;
				rightHand.state = Hand.DESCANZANDO;
				leftHand.selecciona(83);
				rightHand.descanza();
			}
			else{
				leftHand.state = Hand.DESCANZANDO;
				rightHand.state = Hand.SELECCIONANDO;
				rightHand.selecciona(83);
				leftHand.descanza();
			}
		}
		else if( currentOption == 0 && VideoPoker.currentWin == 0 ){
			rightHand.state = Hand.DESCANZANDO;
			leftHand.state = Hand.SELECCIONANDO;
			rightHand.descanza();
			leftHand.selecciona(8);
		}
		else if( currentOption == 2 && VideoPoker.currentWin == 0 ){
			rightHand.state = Hand.SELECCIONANDO;
			leftHand.state = Hand.DESCANZANDO;
			rightHand.selecciona(157);
			leftHand.descanza();
		}
		
		else if(  VideoPoker.currentWin > 0 && currentOption == 0 && VideoPoker.waitCounter > 25 ){
			rightHand.state = Hand.DESCANZANDO;
			leftHand.state = Hand.SELECCIONANDO;
			rightHand.descanza();
			leftHand.selecciona(50);
		}
		else if(  VideoPoker.currentWin > 0 && currentOption == 2  && VideoPoker.waitCounter > 25 ){
			rightHand.state = Hand.SELECCIONANDO;
			leftHand.state = Hand.DESCANZANDO;
			rightHand.selecciona(130);
			leftHand.descanza();
		}
		
	}
	
}
